package ru.papergames.battleserver.network.serverpackets.flashuser;

import org.apache.log4j.Logger;

import java.util.Vector;

/**
 * @author: atroshanin
 * Date: 11.04.2010
 * Time: 0:03:45
 */
public class SystemMessage extends PaperGameServerPacket {

    private static final Logger log = Logger.getLogger(SystemMessage.class.getName());

    private static final int TYPE_ZONE_NAME = 7;
    private static final int TYPE_SKILL_NAME = 4;
    private static final int TYPE_ITEM_NAME = 3;
    private static final int TYPE_NPC_NAME = 2;
    private static final int TYPE_NUMBER = 1;
    //private static final int TYPE_TEXT = 0; // old non-unicode text
    private static final int TYPE_TEXT = 5;
    private int _messageId;
    private Vector<Integer> _types = new Vector<Integer>();
    private Vector<Object> _values = new Vector<Object>();
    private int _skillLvL = 1;

    public SystemMessage(SystemMessageId messageId) {
        _messageId = messageId.getId();
    }

    @Deprecated
    public SystemMessage(int messageId) {
        _messageId = messageId;
    }

    public static SystemMessage sendString(String msg) {
        // TODO: fix it.
//        SystemMessage sm = new SystemMessage(SystemMessageId.CUSTOM_MESSAGE);
//        sm.addString(msg);
//
//        return sm;
        return null;
    }

    public SystemMessage addString(String text) {
        _types.add(TYPE_TEXT);
        _values.add(text);

        return this;
    }

    public SystemMessage addNumber(int number) {
        _types.add(TYPE_NUMBER);
        _values.add(number);
        return this;
    }

    public SystemMessage addNpcName(int id) {
        _types.add(TYPE_NPC_NAME);
        _values.add(1000000 + id);

        return this;
    }

    public SystemMessage addItemName(int id) {
        _types.add(TYPE_ITEM_NAME);
        _values.add(id);

        return this;
    }

/*
// TODO: fix it.
    public SystemMessage addItemName(ItemInstance item) {
        _types.add(TYPE_ITEM_NAME);
        _values.add(item.getItemId());

        return this;
    }
*/


    public SystemMessage addZoneName(int x, int y, int z) {
        _types.add(TYPE_ZONE_NAME);
        int[] coord = {x, y, z};
        _values.add(coord);

        return this;
    }

/*
// TODO: fix it.
    public SystemMessage addSkillName(Skill skill) {
        return addSkillName(skill.getId(), skill.getLevel());
    }
*/

    public SystemMessage addSkillName(int id) {
        return addSkillName(id, 1);
    }

    public SystemMessage addSkillName(int id, int lvl) {
        _types.add(TYPE_SKILL_NAME);
        _values.add(id);
        _skillLvL = lvl;

        return this;
    }

    @Override
    protected int getPacketId() {
        return SYSTEM_MESSAGE_PACKET_ID;
    }

    protected final void writePacketData() {
        log.info("SystemMessage.writePacketData: " + _messageId);
        writeD(_messageId);
        writeD(_types.size());

        for (int i = 0; i < _types.size(); i++) {
            int t = _types.get(i);

            writeD(t);

            switch (t) {
                case TYPE_TEXT: {
                    // TODO: fix it.
                    //writeWS((String) _values.get(i));
                    break;
                }
                case TYPE_NUMBER:
                case TYPE_NPC_NAME:
                case TYPE_ITEM_NAME: {
                    int t1 = (Integer) _values.get(i);
                    writeD(t1);
                    break;
                }
                case TYPE_SKILL_NAME: {
                    int t1 = (Integer) _values.get(i);
                    writeD(t1); // Skill Id
                    writeD(_skillLvL); // Skill lvl
                    break;
                }
                case TYPE_ZONE_NAME: {
                    int t1 = ((int[]) _values.get(i))[0];
                    int t2 = ((int[]) _values.get(i))[1];
                    int t3 = ((int[]) _values.get(i))[2];
                    writeD(t1);
                    writeD(t2);
                    writeD(t3);
                    break;
                }
            }
        }
    }

    public int getMessageID() {
        return _messageId;
    }
}

